package org.ssp.util;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;

import org.ssp.pojo.EmailObject;
import com.sun.mail.util.MailSSLSocketFactory;

/**
 * 邮件发送工具类
 * 
 *  普通邮件和带附件的邮件
 * @author susp
 *
 */
public class SendEmailUtil {
	public static void sendMail(EmailObject mailobj){
		Properties prop=new Properties();
		prop.setProperty("mail.host", "smtp.qq.com");
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.smtp.auth", "true");
		MailSSLSocketFactory sf=null;
		//ssl加密
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);

		} catch (GeneralSecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		Session session=Session.getInstance(prop);
		try {
			Transport tp=session.getTransport();
			tp.connect(prop.getProperty("mail.host"),mailobj.getUsername(),mailobj.getPassword());
			tp.sendMessage(mailobj.createMail(session), mailobj.createMail(session).getAllRecipients());
			tp.close();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
